package nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

/**
 * Created by Administrator on 2017/10/20 0020.
 */
public class FileChannelDemo
{
    public static void main(String[] args) throws Exception
    {
        File f = new File("D:\\Android\\idea_ws\\src\\nio\\1.txt");
        File fout = new File("D:\\Android\\idea_ws\\src\\nio\\2.txt");
        FileChannel fcin = new FileInputStream(f).getChannel();
        FileChannel fcout = new FileOutputStream(fout).getChannel();
        MappedByteBuffer buf = fcin.map(FileChannel.MapMode.READ_ONLY, 0, f.length());
        Charset charset = Charset.forName("GBK");
        fcout.write(buf);
        buf.clear();
        CharsetDecoder charsetDecoder = charset.newDecoder();
        CharBuffer charBuffer = charsetDecoder.decode(buf);
        System.out.println(charBuffer);
    }
}
